﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class OrderTrackingList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F7952;
        protected GridView gvOrderTracking;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblOrderTracking;
        protected LinkButton lnkbtnDelivery;
        protected LinkButton lnkbtnImportData;
        protected LinkButton lnkbtnLost;
        protected LinkButton lnkbtnRejected;
        protected LinkButton lnkbtnSettlement;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnDelivery;
        protected HtmlTableCell tdlnkbtnImportData;
        protected HtmlTableCell tdlnkbtnLost;
        protected HtmlTableCell tdlnkbtnRejected;
        protected HtmlTableCell tdlnkbtnSettlement;

        private void BindData2OrderList(OrdersQueryEntity ordersQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(ordersQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetOrdersInfoByQueryEntity(ordersQuery);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                base.BindData2GridView(this.gvOrderTracking, ds);
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrderTracking, "ORDER_ID");
            if ((OrderID == null) || (OrderID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_014");
            }
            else if (OrderID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_015");
            }
            else
            {
                base.Response.Redirect(string.Format("OrderTracking.aspx?from=list&ID={0}", base.UrlEncode(OrderID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity ordersQuery = this.GetOrderQueryEntity();
            ordersQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = ordersQuery.CurrentPage;
            ordersQuery.CurrentPage = "0";
            this.gvOrderTracking.Columns[1].Visible = false;
            this.gvOrderTracking.AllowSorting = false;
            this.BindData2OrderList(ordersQuery);
            this.hdnActionID.Value = "";
            this.gvOrderTracking.ShowFooter = false;
            ordersQuery.IsGetAll = false;
            ordersQuery.CurrentPage = currentPageIndex;
            this.SetOrderQueryEntity(ordersQuery);
            base.Export2Excel("OrderTracking.xls", this.gvOrderTracking);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            OrdersQuery.CurrentPage = "0";
            this.BindData2OrderList(OrdersQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            OrdersQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2OrderList(OrdersQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            orderQuery.OrderID = "";
            orderQuery.Operator = "";
            orderQuery.OperatingDate = "";
            orderQuery.Type = "";
            orderQuery.Status = "";
            this.BindData2OrderList(orderQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            if (int.Parse(OrdersQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                OrdersQuery.CurrentPage = (int.Parse(OrdersQuery.CurrentPage) + 1).ToString();
                this.BindData2OrderList(OrdersQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            if (int.Parse(OrdersQuery.CurrentPage) > 0)
            {
                OrdersQuery.CurrentPage = (int.Parse(OrdersQuery.CurrentPage) - 1).ToString();
                this.BindData2OrderList(OrdersQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }

        private OrdersQueryEntity GetOrderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as OrdersQueryEntity);
            }
            return (base.PageQueryEntitySession as OrdersQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
                OrdersQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2OrderList(OrdersQuery);
            }
        }

        protected void gvOrderTracking_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7952", this.gvOrderTracking);
        }

        protected void gvOrderTracking_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblABNORMAL"));
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblHANDLING"));
                }
                base.BindingOrdersPriority((Image) e.Row.FindControl("imgPriority"), (Label) e.Row.FindControl("lblPriority"), this.gvOrderTracking.DataKeys[e.Row.RowIndex]["PRIORITY"].ToString(), (string) this.ViewState["PriorityFlag"]);
            }
        }

        protected void gvOrderTracking_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                OrdersQueryEntity ordersQuery = this.GetOrderQueryEntity();
                ordersQuery.SortField = e.SortExpression;
                if (ordersQuery.SortDirection == "ASC")
                {
                    ordersQuery.SortDirection = "DESC";
                }
                else if (ordersQuery.SortDirection == "DESC")
                {
                    ordersQuery.SortDirection = "ASC";
                }
                this.BindData2OrderList(ordersQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnDelivery_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrderTracking, "ORDER_ID");
                List<string> Status = base.GetSelectedIDsInGridView(this.gvOrderTracking, "STATUS");
                if ((OrderID == null) || (OrderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7600_014");
                }
                else
                {
                    for (int i = 0; i < OrderID.Count; i++)
                    {
                        if (Status[i].ToUpper() != "SHIPPED")
                        {
                            this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                            return;
                        }
                        this.lblMsg.Text = "正常妥投" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Delivery", "", DateTime.Now, "", "", new List<string>(), new List<decimal>()) + "状态更新成功!";
                        this.btnRefresh_Click(null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnLost_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrderTracking, "ORDER_ID");
                List<string> Status = base.GetSelectedIDsInGridView(this.gvOrderTracking, "STATUS");
                if ((OrderID == null) || (OrderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7600_014");
                }
                else
                {
                    for (int i = 0; i < OrderID.Count; i++)
                    {
                        if (Status[i].ToUpper() != "SHIPPED")
                        {
                            this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                            return;
                        }
                        this.lblMsg.Text = "全部丢失" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Lost", "", DateTime.Now, "", "", new List<string>(), new List<decimal>()) + "状态更新成功!";
                        this.btnRefresh_Click(null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnRejected_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrderTracking, "ORDER_ID");
                List<string> Status = base.GetSelectedIDsInGridView(this.gvOrderTracking, "STATUS");
                if ((OrderID == null) || (OrderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7600_014");
                }
                else
                {
                    for (int i = 0; i < OrderID.Count; i++)
                    {
                        if (Status[i].ToUpper() != "SHIPPED")
                        {
                            this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                            return;
                        }
                        this.lblMsg.Text = "顾客拒收" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Rejected", "", DateTime.Now, "", "", new List<string>(), new List<decimal>()) + "状态更新成功!";
                        this.btnRefresh_Click(null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnSettlement_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrderTracking, "ORDER_ID");
                List<string> Status = base.GetSelectedIDsInGridView(this.gvOrderTracking, "STATUS");
                if ((OrderID == null) || (OrderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7600_014");
                }
                else
                {
                    for (int i = 0; i < OrderID.Count; i++)
                    {
                        if (Status[i].ToUpper() != "DELIVERY")
                        {
                            this.lblMsg.Text = "只有正常妥投的订单,才允许此操作!";
                            return;
                        }
                        this.lblMsg.Text = "货款结算" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Settlement", "", DateTime.Now, "", "", new List<string>(), new List<decimal>()) + "状态更新成功!";
                        this.btnRefresh_Click(null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            this.ViewState["PriorityFlag"] = base.GetValueFromAppSettings("PriorityFlag");
            OrdersQueryEntity orderQuery = new OrdersQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    orderQuery.OrderID = this.PreviousPage.OrderID;
                    orderQuery.Operator = this.PreviousPage.Operator;
                    orderQuery.OperatingDate = this.PreviousPage.OperatingDate;
                    orderQuery.Type = this.PreviousPage.Type;
                    orderQuery.Status = this.PreviousPage.Status;
                }
                else
                {
                    orderQuery.OrderID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            orderQuery.IsGetAll = false;
            orderQuery.CurrentPage = "0";
            orderQuery.PageSize = this.gvOrderTracking.PageSize.ToString();
            orderQuery.SortField = "O.ORDER_ID";
            orderQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvOrderTracking.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetOrderQueryEntity(orderQuery);
            this.BindData2OrderList(orderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetOrderQueryEntity(OrdersQueryEntity OrdersQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = OrdersQuery;
            }
            else
            {
                base.PageQueryEntitySession = OrdersQuery;
            }
        }

        public OrderTrackingQuery PreviousPage
        {
            get
            {
                return (OrderTrackingQuery) base.PreviousPage;
            }
        }
    }
}

